﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MagicBag.CRM.Model;
using MagicBag.CRM.BLL;
using System.Web;

namespace MagicBag.CRM.Service
{
    public class RoleService : BaseService
    {
        public void Insert(HttpContext context)
        {
            Role role = new Role();

            role.Name = context.Request["Name"];
            role.Remark = context.Request["Remark"];

            Result result = new Result();
            result.Success = true;

            //检测橙色是否已存在

            try
            {
                RoleBLL roleBLL = new RoleBLL();
                roleBLL.Insert(role);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        public void Delete(HttpContext context)
        {
            Result result = new Result();
            result.Success = true;

            int id;
            Int32.TryParse(context.Request["id"], out id);

            try
            {
                RoleBLL roleBLL = new RoleBLL();
                roleBLL.Delete(id);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        public void Update(HttpContext context)
        {
            Role role = new Role();
            int id;
            Int32.TryParse(context.Request["ID"], out id);
            role.ID = id;
            role.Name = context.Request["Name"];
            role.Remark = context.Request["Remark"];


            Result result = new Result();
            result.Success = true;
            try
            {
                RoleBLL roleBLL = new RoleBLL();
                roleBLL.Update(role);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        public void GetRoles(HttpContext context)
        {
            RoleBLL roleBLL = new RoleBLL();

            IList<Role> list = roleBLL.GetRoles();
            string s = string.Empty;

            if (context.Request["ddl"] == "1")//返回角色信息，提供给下拉列表使用
            {
                Role role = new Role();
                role.ID = 0;
                role.Name = "请选择";
                list.Insert(0, role);
                s = JsonHelper.GetJson<IList<Role>>(list);
            }
            else {
                s = GetEasyUIDetailJson<Role>(list, list.Count);
            
            }

            ResponseJson(context, s);
        }
    }
}
